package org.springframework.samples.countries.web.translate;

import java.util.List;
import java.util.Stack;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.objectweb.asm.Type;

import pikes.ecma.FunctionCall;
import pikes.ecma.IAssignmentExpression;
import pikes.ecma.Identifier;
import pikes.ecma.NewExpression;

public class InvokeSpecial implements ByteCodeOperation {

	protected static final Log translatorLogger = LogFactory.getLog("Translator");

	private String owner = null;

	private Type[] args = null;

	public InvokeSpecial(String owner, Type[] args) {
		super();
		this.owner = owner;
		this.args = args;
	}

	public void execute(int numberOfParameters, Stack<Object> stack, List<String> localVariables) {
		translatorLogger.info("INVOKESPECIAL " + owner);

		String simpleName = owner.substring(owner.lastIndexOf("/") + 1);

		FunctionCall functionCall = new FunctionCall(new Identifier(simpleName));
		for (int i = 0; i < args.length; i++) {
			functionCall.addArgument((IAssignmentExpression) stack.pop());
		}

		stack.push(new NewExpression(functionCall));

	}

}
